package advanced.thread;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 线程小列子
 * <p>
 * yield  是 进程礼让 是静态方法 Thread.yield
 * <p>
 * join 是进程插队, 是Thread的成员方法,谁调用join 谁就马上插队先执行
 * (换句话说,在A线程里调用B线程的join方法,那么A线程就会被阻塞,而B就优先执行)
 */
public class L7 {
    public static void main(String[] args) throws InterruptedException {
//        test2.DJS(10);
        System.out.println("爸爸想抽烟,儿子去买烟的故事");
        Father father = new Father();
        new Thread(father).start();
    }

}

class test2 {
    public static void DJS(int seconds) throws InterruptedException {
        //倒计时的时间
        Date start = new Date(System.currentTimeMillis());

        System.out.println("开始倒计时任务时刻 = " + new SimpleDateFormat("hh:mm:ss").format(start));
        Date endTime = new Date(start.getTime() + seconds * 1000);
        long end = endTime.getTime();
        System.out.println("倒计时开始时间=" + new SimpleDateFormat("hh:mm:ss").format(endTime));
        while (true) {
            System.out.println(new SimpleDateFormat("hh:mm:ss").format(endTime));
            Thread.sleep(1000);
            endTime = new Date(endTime.getTime() - 1000);
            if ((end - seconds * 1000) > endTime.getTime()) break;//超过停止时刻,则推出程序

        }
    }
}

class Father extends Thread {
    @Override
    public void run() {
        System.out.println("想抽烟没有烟了");
        System.out.println("叫儿子去买中华");
        Thread t = new Thread(new Son());
        t.start();
        try {
            t.join();//老爸的线程被阻塞,只能等儿子回来才能拿到烟
            System.out.println("老爸接过烟,把零钱给了儿子");
        } catch (InterruptedException e) {
            System.out.println("孩子走丢了,去找孩子了");
            e.printStackTrace();
        }

    }
}

class Son extends Thread {
    @Override
    public void run() {
        System.out.println("结果老爸的钱....");
        System.out.println("路边有个游戏厅玩了10秒");
        for (int i = 0; i < 10; i++) {
            System.out.println((i + 1) + "秒过去了....");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("赶紧买烟去....不然要挨打了");
        System.out.println("手拿一包中华回家了");
    }
}

